package util

import (
	"fmt"
	"runtime"

	"github.com/gin-gonic/gin"
)

type M map[string]interface{}

type JSONRET struct {
	ErrCode int                    `json:"errcode"`
	ErrMsg  string                 `json:"errmsg"`
	Data    map[string]interface{} `json:"data"`
}

func JSON(c *gin.Context, err error, data map[string]interface{}) {
	var resp JSONRET
	if err == nil {
		resp = JSONRET{
			ErrCode: 0,
			ErrMsg:  "",
			Data:    data,
		}
	} else {
		_, file, line, _ := runtime.Caller(1)
		msg := fmt.Sprintf("%s:%d %s", file, line, err)
		Logger.Error(msg)
		resp = JSONRET{
			ErrCode: line,
			ErrMsg:  err.Error(),
			Data:    data,
		}
	}
	c.JSON(200, resp)
}
